
//////////////////////////////////////////////////////////////////////////////////
//	WiiMoteDetection.cs
//  establish a connection to the WiimoteLib
//	Written by Cosic Nadin and Franz Thomas Reinisch FH-Joanneum ITM06
//  
//////////////////////////////////////////////////////////////////////////////////


using System;
using System.Collections.Generic;
using System.Text;
using WiimoteLib;

namespace WiiFlipChart
{
    class WiiMoteDetection
    {
        
        public Wiimote wm;

        public event WiimoteChangedEventHandler WiimoteChanged;



        static readonly WiiMoteDetection instance = new WiiMoteDetection();

        public static WiiMoteDetection Instance
        {
            get
            {
                return instance;
            }
        }




        public WiiMoteDetection()
        {
            
            // create a new instance of the Wiimote
            wm = new Wiimote();

           wm.WiimoteChanged += new WiimoteChangedEventHandler(OnWiimoteChanged);


            // setup the event to handle insertion/removal of extensions
            wm.WiimoteExtensionChanged += new WiimoteExtensionChangedEventHandler(wm_OnWiimoteExtensionChanged);

     

            try
            {
                if (wm.Equals(null)) throw new Exception("no wii mote");
                wm.Connect();
            }

            catch (Exception e)
            {
                Console.WriteLine("Wii Failure"); 
            } 



            wm.SetReportType(Wiimote.InputReport.IRAccel, true);
            wm.SetLEDs(true , false, false, false);



            
        }



        protected void OnWiimoteChanged(object sender, WiimoteChangedEventArgs args)
        {
            if (WiimoteChanged != null)
           {
                WiimoteChanged(sender, args);
           }
        }



        void wm_OnWiimoteExtensionChanged(object sender, WiimoteExtensionChangedEventArgs args)
        {
            if (args.Inserted)
                wm.SetReportType(Wiimote.InputReport.IRExtensionAccel, true);    // return extension data
            else
                wm.SetReportType(Wiimote.InputReport.IRAccel, true);    // back to original mode
        }




    }
}
